@echo off
setlocal enabledelayedexpansion

REM ------------------------------------------------------------------
REM Update PPH-Production-Deploy from remote and restart PM2 services
REM Pure ASCII, safe for Windows CMD (no UTF-8 BOM, no Chinese chars)
REM Usage:
REM   update-from-remote-en.bat [REPO_DIR]
REM Example:
REM   update-from-remote-en.bat D:\PPH-Production-Deploy
REM If REPO_DIR is omitted, current directory will be used. If current
REM directory is not the repo, it tries D:\PPH-Production-Deploy.
REM Requirements: Git, Node.js v18.17.1, npm, PM2
REM ------------------------------------------------------------------

set "ORIGIN_URL=https://gitee.com/rain-spirit-z/pph-production-system.git"
set "REPO_DIR=%~1"
if "%REPO_DIR%"=="" set "REPO_DIR=%cd%"
if not exist "%REPO_DIR%\config\ecosystem.config.cjs" (
  if exist "D:\PPH-Production-Deploy\config\ecosystem.config.cjs" (
    set "REPO_DIR=D:\PPH-Production-Deploy"
  )
)

echo === PPH update script ===
echo Repo Dir: %REPO_DIR%
echo Origin  : %ORIGIN_URL%

if not exist "%REPO_DIR%\config\ecosystem.config.cjs" (
  echo [ERROR] REPO_DIR is not a valid PPH-Production-Deploy folder.
  exit /b 2
)

pushd "%REPO_DIR%" >nul 2>&1
if errorlevel 1 (
  echo [ERROR] Cannot change directory to %REPO_DIR%.
  exit /b 3
)

REM 0) quick environment checks
where git >nul 2>&1 || (echo [ERROR] git not found in PATH.& exit /b 10)
where node >nul 2>&1 || (echo [ERROR] node not found in PATH.& exit /b 11)
where npm  >nul 2>&1 || (echo [ERROR] npm  not found in PATH.& exit /b 12)
where npx  >nul 2>&1 || (echo [WARN ] npx not found, pm2 commands may fail.)

REM Node version advisory (do not hard fail, only warn)
for /f "tokens=*" %%v in ('node --version') do set "NODEV=%%v"
echo Node Version: %NODEV%

REM 1) ensure remote and safe.directory
echo.
echo [STEP] Configure git remote and safe.directory
git remote -v
for /f "tokens=2" %%u in ('git remote -v ^| findstr /i "^origin" ^| findstr /i "(fetch)"') do set "CUR_ORIGIN=%%u"
if /i not "%CUR_ORIGIN%"=="%ORIGIN_URL%" (
  echo Setting origin to %ORIGIN_URL%
  git remote set-url origin "%ORIGIN_URL%" || (echo [ERROR] set-url failed.& exit /b 20)
)
git config --global --add safe.directory "%REPO_DIR%" >nul 2>&1

REM 2) stash local changes if any
echo.
echo [STEP] Check local changes
git diff-index --quiet HEAD -- || (
  echo Detected local changes. Stashing...
  git stash push -u -m "pre-update %date% %time%" || (echo [ERROR] git stash failed.& exit /b 21)
)

REM 3) fetch/pull latest master
echo.
echo [STEP] Fetch and pull latest master
git fetch --all --prune || (echo [ERROR] git fetch failed.& exit /b 22)
git checkout master || (echo [ERROR] checkout master failed.& exit /b 23)
git pull --rebase origin master || (echo [ERROR] git pull --rebase failed.& exit /b 24)

REM 4) install production deps when necessary (backend, management)
echo.
echo [STEP] Install server dependencies (production)
pushd "%REPO_DIR%\app\backend" >nul && (
  call npm ci --only=production || call npm install --production || (echo [ERROR] backend deps install failed.& exit /b 30)
  popd >nul
) || (echo [ERROR] backend dir missing.& exit /b 31)

pushd "%REPO_DIR%\management" >nul && (
  call npm ci --only=production || call npm install --production || (echo [WARN ] management deps install failed.)
  popd >nul
)

REM 5) build frontend
echo.
echo [STEP] Build frontend
pushd "%REPO_DIR%\app\frontend" >nul && (
  call npm run build || (echo [ERROR] frontend build failed.& exit /b 40)
  popd >nul
) || (echo [ERROR] frontend dir missing.& exit /b 41)

REM 6) restart services and save
echo.
echo [STEP] Restart PM2 services
call npx pm2 restart pph-backend || echo [WARN ] restart pph-backend failed.
call npx pm2 restart pph-frontend || echo [WARN ] restart pph-frontend failed.
call npx pm2 restart pph-management || echo [WARN ] restart pph-management failed.
call npx pm2 save || echo [WARN ] pm2 save failed.

echo.
echo [DONE] Update completed. If browser shows old UI, force refresh twice (Ctrl+F5)
echo        or clear Service Worker: DevTools -> Application -> Service Workers -> Unregister.

popd >nul
endlocal
exit /b 0

